package com.design.mode.single;

import org.junit.jupiter.api.Test;

import java.io.*;
import java.lang.reflect.Constructor;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-04-03 16:05:14
 */
public class CrackTest {

    @Test
    public void test3() {
        Single01 instance = Single01.getInstance();
        System.out.println("instance = " + instance);
    }

    @Test
    public void test1() throws Exception {
        Single01 s1 = Single01.getInstance();

        Class<? extends Single01> cls = s1.getClass();
        Constructor<? extends Single01> constructor = cls.getDeclaredConstructor();
        constructor.setAccessible(true);
        Single01 s2 = constructor.newInstance();

        System.out.println("s1.hashCode() = " + s1.hashCode());
        System.out.println("s2.hashCode() = " + s2.hashCode());
    }

    @Test
    public void test2() throws Exception {
        Single01 s1 = Single01.getInstance();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(s1);

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        Single01 s2 = (Single01) ois.readObject();

        System.out.println("s1.hashCode() = " + s1.hashCode());
        System.out.println("s2.hashCode() = " + s2.hashCode());
    }

}
